package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.IProductsDao;
import com.cn.wanxi.dao.impl.ProductsDaoImpl;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductsService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class ProductsServiceImpl implements IProductsService {
    @Override
    public int add(ProductModel productModel) {
        IProductsDao iProductDao=new ProductsDaoImpl();
        int count=iProductDao.add(productModel);
        if (count==1){
            Jedis jedis=new Jedis();
            jedis.del("products_id");
            jedis.del("products_title");
            jedis.del("products_imgHref");
            jedis.del("products_content");
            jedis.del("products_contentcontent");
            jedis.del("products_casetypename");
        }
        return count;
    }

    @Override
    public List<ProductModel> findAll(PageModel pageModel) {
        IProductsDao iProductDao=new ProductsDaoImpl();
        List<ProductModel> list=new ArrayList<>();
        Jedis jedis=new Jedis();
        Long length=jedis.llen("products_id");
        if (pageModel.getPageSize()!=10 || pageModel.getPageNum()!=1){//注意整形用=号
            list=iProductDao.findAll(pageModel);
        }else  if(length>0){
            List<String> id=jedis.lrange("products_id",0,length);
            List<String> title=jedis.lrange("products_title",0,length);
            List<String> imgHref=jedis.lrange("products_imgHref",0,length);
            List<String> content=jedis.lrange("products_content",0,length);
            List<String> contentcontent=jedis.lrange("products_contentcontent",0,length);
            List<String> casetypename=jedis.lrange("products_casetypename",0,length);
            for (int i=0;i<length;i++){
                ProductModel model=new ProductModel();
                model.setId(Integer.parseInt(id.get(i)));
                model.setTitle(title.get(i));
                model.setImgHref(imgHref.get(i));
                model.setContent(content.get(i));
                model.setContentcontent(contentcontent.get(i));
                model.setCasetypename(casetypename.get(i));
                list.add(model);
            }
        }else {
            list=iProductDao.findAll(pageModel);
            for (ProductModel model:list) {
                jedis.rpush("products_id", model.getId().toString());
                jedis.rpush("products_title",model.getTitle());
                jedis.rpush("products_imgHref",model.getImgHref());
                jedis.rpush("products_content",model.getContent());
                jedis.rpush("products_contentcontent",model.getContentcontent());
                jedis.rpush("products_casetypename",model.getCasetypename());
            }
        }
        return list;
    }


    @Override
    public List<ProductModel> productsFind() {
        IProductsDao iProductDao=new ProductsDaoImpl();
        List<ProductModel> list=new ArrayList<>();
        Jedis jedis=new Jedis();
        Long length=jedis.llen("products_id");
       if(length>0){
            List<String> id=jedis.lrange("products_id",0,length);
            List<String> title=jedis.lrange("products_title",0,length);
            List<String> imgHref=jedis.lrange("products_imgHref",0,length);
            List<String> content=jedis.lrange("products_content",0,length);
            List<String> contentcontent=jedis.lrange("products_contentcontent",0,length);
            List<String> casetypename=jedis.lrange("products_casetypename",0,length);
            for (int i=0;i<length;i++){
                ProductModel model=new ProductModel();
                model.setId(Integer.parseInt(id.get(i)));
                model.setTitle(title.get(i));
                model.setImgHref(imgHref.get(i));
                model.setContent(content.get(i));
                model.setContentcontent(contentcontent.get(i));
                model.setCasetypename(casetypename.get(i));
                list.add(model);
            }
        }else {
            list=iProductDao.productsFind();
            for (ProductModel model:list) {
                jedis.rpush("products_id", model.getId().toString());
                jedis.rpush("products_title",model.getTitle());
                jedis.rpush("products_imgHref",model.getImgHref());
                jedis.rpush("products_content",model.getContent());
                jedis.rpush("products_contentcontent",model.getContentcontent());
                jedis.rpush("products_casetypename",model.getCasetypename());
            }
        }
        return list;
    }


    @Override
    public List<ProductModel> productsFinds(ProductModel productModel) {
        IProductsDao iProductDao=new ProductsDaoImpl();
        List<ProductModel> list=iProductDao.productsFinds(productModel);
        return list;
    }

    @Override
    public List<ProductModel> productsTypeFind() {
        IProductsDao iProductDao=new ProductsDaoImpl();
        List<ProductModel> list=iProductDao.productsTypeFind();
        return list;
    }

    @Override
    public List<ProductModel> idFindProduct(ProductModel productModel) {
        IProductsDao iProductDao=new ProductsDaoImpl();
        List<ProductModel> list=iProductDao.idFindProduct(productModel);
        return list;
    }

    @Override
    public int getPageCount(ProductModel productModel) {
        IProductsDao iProductDao=new ProductsDaoImpl();
        int count=iProductDao.getPageCount(productModel);
        return count;
    }


    @Override
    public int deletebyid(int aa) {
        IProductsDao iProductDao=new ProductsDaoImpl();
        int count= iProductDao.deletebyid(aa);
        if (count==1){
            Jedis jedis=new Jedis();
            jedis.del("products_id");
            jedis.del("products_title");
            jedis.del("products_imgHref");
            jedis.del("products_content");
            jedis.del("products_contentcontent");
            jedis.del("products_casetypename");
        }
        return count;
    }

    @Override
    public ProductModel findbyId(int productid) {
        IProductsDao iProductDao=new ProductsDaoImpl();
        ProductModel productModel=iProductDao.findbyId(productid);
        CaseTypeServiceImpl caseTypeService=new CaseTypeServiceImpl();
        productModel.setList(caseTypeService.CaseTypeFindAll());
        return productModel;
    }

    @Override
    public int update(ProductModel productModel) {
        IProductsDao iProductDao=new ProductsDaoImpl();
        int count= iProductDao.update(productModel);
        if (count==1){
            Jedis jedis=new Jedis();
            jedis.del("products_id");
            jedis.del("products_title");
            jedis.del("products_imgHref");
            jedis.del("products_content");
            jedis.del("products_contentcontent");
            jedis.del("products_casetypename");
        }
        return count;
    }

    @Override
    public int getCount(ProductModel productModel) {
        IProductsDao iProductDao=new ProductsDaoImpl();
        return iProductDao.getCount(productModel);
    }

    @Override
    public List<ProductModel> productTh(ProductModel productModel) {
        IProductsDao iProductDao=new ProductsDaoImpl();
        List<ProductModel> list=iProductDao.productTh(productModel);
        return list;
    }

}











